//******************************************************************************************************
// Copyright 2008 Amazon Technologies, Inc.  
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 
// compliance with the License. 
//
// You may obtain a copy of the License at:http://aws.amazon.com/apache2.0  This file is distributed on 
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
//
// See the License for the specific language governing permissions and limitations under the License. 
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Text;

using System.Web.Services;
using System.Net;
using System.IO;

namespace WinFormCD.Infrastructure.Interface.BusinessEntities
{
    public class StockQuote
    {
        string   _symbol;
        string   _last;
        DateTime _date;
        string   _change;
        string   _open;
        string   _high;
        string   _low;
        string   _volume;
        string   _marketCap;
        string   _previousClose;
        string   _percentChange;
        string   _annualRange;
        string   _earnings;
        string   _peRatio;
        string   _name;
        
        /// <summary>
        /// Used to parse stock quote data
        /// </summary>
        private static char[] delimiter = {','};
        
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="quoteData">Comma delimited string of quote data</param>
        public StockQuote(string quoteData)
        {
            // now should have clean string, create array of elements
            string[] contents = quoteData.Split(delimiter);

            this._symbol         = contents[0];
            this._last           = contents[1];
            this._date           = DateTime.Now;
            this._change         = contents[4];
            this._open           = contents[5];
            this._high           = contents[6];
            this._low            = contents[7];
            this._volume         = contents[8];
            this._marketCap      = contents[9];
            this._previousClose  = contents[10];
            this._percentChange  = contents[11];
            this._annualRange    = contents[13];
            this._earnings       = contents[14];
            this._peRatio        = contents[15];
            this._name           = contents[16];
        }
 
        public String Symbol
        {
            get { return _symbol; }
        }

        public String Last
        {
            get { return _last; }
        }

        public String Change
        {
            get { return _change; }
        }

        public String PercentChange
        {
            get { return _percentChange; }
        }

        public String High
        {
            get { return _high; }
        }

        public String Low
        {
            get { return _low; }
        }

        public DateTime Date
        {
            get { return _date; }
        }

        public String Name
        {
            get { return _name; }
        }

        public String Volume
        {
            get { return _volume; }
        }

        /// <summary>
        /// Return the data for a stock symbol; the lookup will be done via Yahoo
        /// </summary>
        /// <param name="requestedSymbol">Name of the symbol</param>
        /// <returns></returns>
        public static string Retrieve(string requestedSymbol)
        {
            //
            //URL to Yahoo spreadsheet format stock quote server...
            //
            string serverURL = string.Format("http://quote.yahoo.com/d/quotes.csv?f=sl1d1t1c1ohgvj1pp2owern&e=.csv&s={0}", requestedSymbol);

            HttpWebRequest  webRequest  = (HttpWebRequest) WebRequest.Create(serverURL);
            HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();
            StreamReader    reader    = new StreamReader(webResponse.GetResponseStream(), Encoding.ASCII);

            string quoteLine = "";
            quoteLine = reader.ReadLine();
            quoteLine = quoteLine.Replace("\"", "");
            
            reader.Close();

            return quoteLine;
        }
    
        /// <summary>
        /// Given a price, return its representation as a double with 2 decimal places.
        /// </summary>
        /// <param name="v">Price as a string</param>
        /// <returns></returns>
        public static string FormatPrice(string v)
        {
            string result = v;

            try
            {
                double p = Convert.ToDouble(v);
                result = String.Format("{0:N2}", p);
            }
            
            catch {}
            
            return result;
        }

        /// <summary>
        /// Format a volume from a long to a string
        /// </summary>
        /// <param name="v">String value of volume</param>
        /// <returns></returns>
        public static string FormatVolume(string v)
        {
            string result = v;
            try
            {
                long vol = Convert.ToInt64(v);
                result = String.Format("{0:N0}", vol);
            }
            catch {}
            return result;
        }

    }
}
